home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / cisco / tacacsd.shar / xtacacsd / changetac.c next >
Encoding:
C/C++ Source or Header  |  1992-08-07  |  17.1 KB  |  649 lines

  1. /*
  2.  * changetac
  3.  *
  4.  * This program maintains the /etc/tacpasswd file.  It allows users to
  5.  * change their own TACACS entries.
  6.  *
  7.  * The password generator routines are from Brandon Allbery, as
  8.  * distribured in  comp.sources.misc, volume 5, pwgen.
  9.  *
  10.  * Written by: David Hampton, 20 February 1990.
  11.  * From the tacpaswd program written by: David Hampton, 30 June 89.
  12.  */
  13.  
  14. #ifdef SYSV
  15. #define random rand
  16. #define srandom srand
  17. #endif
  18.  
  19. /*
  20. Path: xanth!mcnc!gatech!cwjcc!hal!ncoast!allbery
  21. From: allbery@ncoast.UUCP (Brandon S. Allbery)
  22. Newsgroups: comp.sources.misc
  23. Subject: v05i059: pwgen -- random-but-pronounceable password generator
  24. Message-ID: <13184@ncoast.UUCP>
  25. Date: 26 Nov 88 04:44:18 GMT
  26. Sender: allbery@ncoast.UUCP
  27. Reply-To: allbery@ncoast.UUCP (Brandon S. Allbery)
  28. Organization: Cleveland Public Access UN*X, Cleveland, Oh
  29. Lines: 328
  30. Approved: allbery@ncoast.UUCP
  31.  
  32. Posting-number: Volume 5, Issue 59
  33. Submitted-by: "Brandon S. Allbery" <allbery@ncoast.UUCP>
  34. Archive-name: pwgen
  35.  
  36. [Dr. Jekyll and Mr. Hyde, anyone?  ;-)  ++bsa]
  37.  
  38. I've described this program, in its OSI Superboard-II incarnation, in
  39. comp.unix.wizards and news.sysadmin.  Now, here's the code for a UN*X
  40. version.  It seems to work OK on ncoast, except for some oddness in the
  41. "random" selections -- which are probably the result of our not having a
  42. good RNG.  (Could someone mail me one or more of the alternative
  43. generators?  Or maybe even the source to BSD random(), assuming that it
  44. falls outside the subset of BSD code that can be traced to AT&T?  To put it
  45. mildly, our random number generator isn't.)
  46.  
  47. This is not as complete as the original program, which actually had a list
  48. of characters (the current one uses phonemes) which were most likely to
  49. follow other phonemes.  On the other hand, this version does use phonemes
  50. rather than characters, so its creations are more pronounceable as a rule
  51. than those of the original.  The resulting passwords aren't quite as
  52. "natural" as the original, however.  (That may be for the best; the original
  53. was intended as a simple experiment to see if a rule for "proper English
  54. words" could be defined in terms of rules which related letters.  This was
  55. before I got to college and learned that such things were rather unlikely,
  56. but the original program still did a pretty good job of spewing out some
  57. common English words.  That can't be said for *this* program.)
  58.  
  59. To compile:    cc -O -o pwgen pwgen.c -lm
  60. (The sqrt() function is used in an attempt to produce a random number which
  61. is "weighted" toward one end of the range, so as to prefer one end of a list
  62. of "spellings" for a phoneme over the other end.  It seems to work, but with
  63. this rotted RNG, I can't be absolutely certain.  A trial distribution seems
  64. to be correct, however.)
  65.  
  66. What's the intent?  I find that I can remember a "word" better if I can
  67. pronounce it.  This may or may not be true for other people, but *anything*
  68. that encourages the use of random passwords is an improvement on what I
  69. typically see at client sites every day.  So this program spits out
  70. passwords which are virtually guaranteed not to be found in the dictionary,
  71. but are (usually) pronounceable to speakers of English.  (The tables can be
  72. modified for other languages, but they're rather hairy.  Perhaps I'll write
  73. a version which loads "compiled" language descriptions, and let the compiler
  74. deal with the hairy stuff.  Hey, they were even hairier in the BASIC
  75. version!)
  76.  
  77. Oh, well, shar and enjoy.
  78.  
  79. ++Brandon, sitting on the other side of the fence for the moment
  80. */
  81.  
  82. /*
  83.  * Generate (hopefully) pronounceable random passwords.  These can often be
  84.  * remembered more easily than completely random passwords, and are immune to
  85.  * dictionary searches, etc.
  86.  *
  87.  * The original version of this program was written in BASIC on an OSI
  88.  * Superboard II SBC.  That version is long gone (the SB2's cassette drive
  89.  * was never trustworthy, it eventually scrambled the tape), but the basic
  90.  * (pardon the pun) idea lives on here, with a few modification like basing
  91.  * the selection on "graphs" (actually, they are a restricted set of phonemes)
  92.  * and having randomly-selected spellings for those graphs.
  93.  */
  94.  
  95. #include <stdio.h>
  96. #include <math.h>
  97.  
  98. #define RANDOM(c)    ((int) (((random(c) & 0x7fff) / 32767.0) * (c)))
  99.  
  100. char *spelling[] = {
  101. /*a*/    "a",                (char *) 0,    /* 2*/
  102. /*A*/    "a",    "ae",    "ai",        (char *) 0,    /* 6*/
  103. /*b*/    "b",                (char *) 0,    /* 8*/
  104. /*ch*/    "ch",                (char *) 0,    /*10*/
  105. /*d*/    "d",                (char *) 0,    /*12*/
  106. /*e*/    "e",                (char *) 0,    /*14*/
  107. /*E*/    "e",    "ee",    "ie",        (char *) 0,    /*18*/
  108. /*f*/    "f",    "ph",    "gh",        (char *) 0,    /*22*/
  109. /*g*/    "g",                (char *) 0,    /*24*/
  110. /*h*/    "h",                (char *) 0,    /*26*/
  111. /*i*/    "i",    "e",            (char *) 0,    /*29*/
  112. /*I*/    "i",    "ai",            (char *) 0,    /*32*/
  113. /*i'*/    "i",    "ei",            (char *) 0,    /*35*/
  114. /*j*/    "j",    "g",            (char *) 0,    /*38*/
  115. /*k*/    "k",    "c",            (char *) 0,    /*41*/
  116. /*l*/    "l",                (char *) 0,    /*43*/
  117. /*m*/    "m",                (char *) 0,    /*45*/
  118. /*n*/    "n",                (char *) 0,    /*47*/
  119. /*ng*/    "ng",                (char *) 0,    /*49*/
  120. /*o*/    "o",    "a",    "ah",        (char *) 0,    /*53*/
  121. /*O*/    "o",    "oh",            (char *) 0,    /*56*/
  122. /*oo*/    "oo",    "u",            (char *) 0,    /*59*/
  123. /*OO*/    "oo",    "w",            (char *) 0,    /*62*/
  124. /*p*/    "p",                (char *) 0,    /*64*/
  125. /*qu*/    "qu",                (char *) 0,    /*66*/
  126. /*r*/    "r",                (char *) 0,    /*68*/
  127. /*s*/    "s",    "c",            (char *) 0,    /*71*/
  128. /*sh*/    "sh",    "s",            (char *) 0,    /*74*/
  129. /*t*/    "t",                (char *) 0,    /*76*/
  130. /*th*/    "th",                (char *) 0,    /*78*/
  131. /*TH*/    "th",                (char *) 0,    /*80*/
  132. /*u*/    "u",                (char *) 0,    /*82*/
  133. /*U*/    "u",    "oo",            (char *) 0,    /*85*/
  134. /*v*/    "v",                (char *) 0,    /*87*/
  135. /*x*/    "x",                (char *) 0,    /*89*/
  136. /*y*/    "y",                (char *) 0,    /*91*/
  137. /*z*/    "z",    "s",            (char *) 0,    /*94*/
  138. };
  139.  
  140. struct graph {
  141.     char *graph;
  142.     char type;
  143. #define CONSONANT    0
  144. #define VOWEL_LONG    1
  145. #define VOWEL_SHORT    2
  146. #define VOWEL_OTHER    3
  147. #define VOWEL_MASK    3
  148. #define iscons(c)    (((c)->type & VOWEL_MASK) == 0)
  149. #define isvowel(c)    (((c)->type & VOWEL_MASK) != 0)
  150. /*    char frequency;            */    /* unused for now */
  151.     char **spellings;
  152. /*    struct graph **following;    */    /* maybe later */
  153. } graph[] = {
  154.     "a",    VOWEL_SHORT,    &spelling[0],
  155.     "A",    VOWEL_LONG,    &spelling[2],
  156.     "b",    CONSONANT,    &spelling[6],
  157.     "ch",    CONSONANT,    &spelling[8],
  158.     "d",    CONSONANT,    &spelling[10],
  159.     "e",    VOWEL_SHORT,    &spelling[12],
  160.     "E",    VOWEL_LONG,    &spelling[14],
  161.     "f",    CONSONANT,    &spelling[18],
  162.     "g",    CONSONANT,    &spelling[22],
  163.     "h",    CONSONANT,    &spelling[24],
  164.     "i",    VOWEL_SHORT,    &spelling[26],
  165.     "I",    VOWEL_LONG,    &spelling[29],
  166.     "i'",    VOWEL_OTHER,    &spelling[32],
  167.     "j",    CONSONANT,    &spelling[35],
  168.     "k",    CONSONANT,    &spelling[38],
  169.     "l",    CONSONANT,    &spelling[41],
  170.     "m",    CONSONANT,    &spelling[43],
  171.     "n",    CONSONANT,    &spelling[45],
  172.     "ng",    CONSONANT,    &spelling[47],
  173.     "o",    VOWEL_SHORT,    &spelling[49],
  174.     "O",    VOWEL_LONG,    &spelling[53],
  175.     "oo",    VOWEL_SHORT,    &spelling[56],
  176.     "OO",    VOWEL_LONG,    &spelling[59],
  177.     "p",    CONSONANT,    &spelling[62],
  178.     "qu",    CONSONANT,    &spelling[64],
  179.     "r",    CONSONANT,    &spelling[66],
  180.     "s",    CONSONANT,    &spelling[68],
  181.     "sh",    CONSONANT,    &spelling[71],
  182.     "t",    CONSONANT,    &spelling[74],
  183.     "th",    CONSONANT,    &spelling[76],
  184.     "TH",    CONSONANT,    &spelling[78],
  185.     "u",    VOWEL_SHORT,    &spelling[80],
  186.     "U",    VOWEL_LONG,    &spelling[82],
  187.     "v",    CONSONANT,    &spelling[85],
  188.     "x",    CONSONANT,    &spelling[87],
  189.     "y",    CONSONANT,    &spelling[89],
  190.     "z",    CONSONANT,    &spelling[91],
  191.     0,    0,        &spelling[94],
  192. };
  193.  
  194. struct graph *vowel[] = {
  195.     &graph[0],    &graph[1],    &graph[5],    &graph[6],
  196.     &graph[10],    &graph[11],    &graph[12],    &graph[19],
  197.     &graph[20],    &graph[21],    &graph[22],    &graph[30],
  198.     &graph[31],
  199.     (struct graph *) 0,
  200. };
  201.  
  202. struct graph *consonant[] = {
  203.     &graph[2],    &graph[3],    &graph[4],    &graph[7],
  204.     &graph[8],    &graph[9],    &graph[13],    &graph[14],
  205.     &graph[15],    &graph[16],    &graph[17],    &graph[18],
  206.     &graph[23],    &graph[24],    &graph[25],    &graph[26],
  207.     &graph[27],    &graph[28],    &graph[29],    &graph[32],
  208.     &graph[33],    &graph[34],    &graph[35],
  209.     (struct graph *) 0,
  210. };
  211.  
  212. /*
  213.  * Randomly select a graph from the specifield array.  Eventually, this should
  214.  * account for graph frequencies as well.
  215.  */
  216.  
  217. struct graph *selgraph(graphs)
  218.     struct graph **graphs;
  219. {
  220.     register int cnt;
  221.  
  222.     for (cnt = 0; graphs[cnt] != (struct graph *) 0; cnt++)
  223.         ;
  224.     return graphs[RANDOM(cnt)];
  225. }
  226.  
  227. /*
  228.  * Randomly select a spelling for the specified graph.  This is not linear:
  229.  * earlier spellings are preferred over later ones, but the latter do
  230.  * sometimes sneak in.
  231.  */
  232.  
  233. char *selspell(graph)
  234.     struct graph *graph;
  235. {
  236.     register int cnt, sel;
  237.  
  238.     for (cnt = 0; graph->spellings[cnt] != (char *) 0; cnt++)
  239.         ;
  240.     if (cnt == 0) {
  241.         fprintf(stderr, "PANIC: selspell(%s) got count(spellings) == 0\n", graph->graph);
  242.         exit(2);
  243.     }
  244.     if (cnt == 1)
  245.         return *graph->spellings;
  246. /*
  247.  * This may not be the best way to do it... maybe Weemba'd care to lend a
  248.  * hand here?  After all, my specialty is programming, NOT math.
  249.  */
  250.     if ((sel = cnt - (int) sqrt((double) RANDOM(cnt * cnt) + 1) - 1) < 0 || sel >= cnt) {
  251. #ifdef BUGCATCH
  252.         fprintf(stderr, "PANIC: selspell(%s) got nlrand(%d) == %d\n", graph->graph, cnt, sel);
  253.         exit(2);
  254. #else
  255.         sel = 0;
  256. #endif
  257.     }
  258.     return graph->spellings[sel];
  259. }
  260.  
  261. /*
  262.  * Choose the next source for a graph.  The rules are:  a consonant MUST be
  263.  * followed by a vowel; a vowel may be followed by a vowel of a different
  264.  * type or by a consonant, but never more than two consecutive vowel graphs.
  265.  */
  266.  
  267. choosenext(cur, prev)
  268. {
  269.     if (cur == CONSONANT)
  270.         return VOWEL_MASK;
  271.     else if (prev == -1 || (prev & VOWEL_MASK) != 0)
  272.         return CONSONANT;
  273.     else if (RANDOM(10) == 5)
  274.         return VOWEL_MASK;
  275.     else
  276.         return CONSONANT;
  277. }
  278.  
  279. /*
  280.  * We are passed an array of (struct graph *); choose an entry randomly and
  281.  * assemble a string fitting the size constraint.  We use the original (OSI)
  282.  * paradigm:  alternate consonants and vowels, with the option of two vowels
  283.  * in a row occasionally.  The only difference is that they must be different
  284.  * *types* of vowels, a distinction that the OSI version didn't consider.
  285.  */
  286.  
  287. pwgen(initial, pw, maxlen)
  288.     struct graph **initial;
  289.     char *pw;
  290. {
  291.     int pwlen, state, prev, tmp;
  292.     struct graph *graph;
  293.     char *spelling;
  294.  
  295.     pwlen = 0;
  296.     state = initial[0]->type;
  297.     prev = -1;
  298.     while (pwlen < maxlen - 1) {
  299.         do {
  300.             graph = selgraph(initial);
  301.         } while (state != CONSONANT && graph->type == prev);
  302.         if ((spelling = selspell(graph)) == (char *) 0) {
  303.             fprintf(stderr, "PANIC: got NULL in selspell(%s)\n", graph->graph);
  304.             exit(2);
  305.         }
  306.         strcpy(pw, spelling);
  307.         while (*pw != '\0')
  308.             pwlen++, pw++;
  309.         tmp = prev;
  310.         prev = graph->type;
  311.         if ((state = choosenext(prev, tmp)) == CONSONANT)
  312.             initial = consonant;
  313.         else
  314.             initial = vowel;
  315.     }
  316. }
  317.  
  318. /*
  319.  * ==============================  End of pwgen  ==============================
  320.  */
  321.  
  322. #include <pwd.h>
  323. #include <ctype.h>
  324. #include <sys/types.h>
  325. #include <sys/stat.h>
  326.  
  327. #define TACPWFILE "./pwfile"
  328. #define TMPPWFILE "/tmp/tacpasswd.XXXXXX"
  329. #define PW_NAMLEN 8                /* max length user name */
  330. #define PW_PWDLEN 8                /* max length password  */
  331. #define GEN_PWDLEN 8                /* length of gen passwd */
  332. #define SPACE_PASSWD 20                /* room allocated for enc passwd */
  333. #define SPACE_DATE 20                /* room allocated for exp date */
  334. #define SHORTTIME 30
  335. #define LONGTIME 180
  336.  
  337. #define FSEEK_F_BEGIN 0
  338. #define FSEEK_F_CURR  1
  339. #define FSEEK_F_END   2
  340.  
  341. /*
  342.  * Global storage
  343.  */
  344. char *whoami;
  345. FILE *pwfile, *tmppwfile;
  346.  
  347. /*
  348.  * Forward declarations
  349.  */
  350. char *mktemp(), *getpass(), *crypt();
  351. struct passwd *fgetpwent();
  352.  
  353. /*
  354.  * my_exit
  355.  *
  356.  * Close and remove the temporary file if necessary, and then exit.
  357.  */
  358. my_exit(code)
  359. int code;
  360. {
  361.     if (tmppwfile)
  362.     fclose(tmppwfile);
  363.     exit(code);
  364. }
  365.  
  366. /*
  367.  * valid
  368.  *
  369.  * Validae this name/password pair against the current record.
  370.  */
  371. valid(entry, name, word)
  372.     struct passwd *entry;
  373.     char *name, *word;
  374. {
  375.     char salt[2];
  376.  
  377.     if (strcmp(name, entry->pw_name) != 0)
  378.     return(0);
  379.  
  380.     strncpy(salt, entry->pw_passwd, 2);
  381.     if (strcmp(entry->pw_passwd, crypt(word, salt)) != 0)
  382.     return(0);
  383.  
  384.     return(1);
  385. }
  386. /*
  387.  * update_expiration
  388.  *
  389.  * Update the expiration date on this entry.  Passwords expire 180 days
  390.  * after they are initially set or changed.
  391.  */
  392. update_expiration(entry)
  393.     struct passwd *entry;
  394. {
  395.     long now, day, year, delta;
  396.     char month[10], timestring[26];
  397.  
  398.     if (entry->pw_shell) {
  399.     if (sscanf(entry->pw_shell, "%s %d %d, %d", month, &day, &year, &delta) != 4)
  400.         return(0);
  401.     free(entry->pw_shell);
  402.     } else
  403.     return(0);
  404.  
  405.     if (delta != 180)
  406.     return(0);
  407.  
  408.     time(&now);
  409.     now += delta * 24 * 60 * 60;
  410.     strcpy(timestring, ctime(&now));
  411.  
  412.     entry->pw_shell = (char *)malloc(SPACE_DATE);
  413.     sprintf(entry->pw_shell, "%6.6s %4.4s, %d", timestring+4, timestring+20,
  414.         delta);
  415.  
  416.     return(1);
  417. }
  418.  
  419. /*
  420.  * make_passwd
  421.  *
  422.  * This routine sets up and calls the password generation routines.  It
  423.  * encrypts the result, and stores it in the password field of the
  424.  * structure.
  425.  */
  426. make_passwd(entry)
  427.     struct passwd *entry;
  428. {
  429.     char genpasswd[20], salt[2];
  430.  
  431.     pwgen((RANDOM(10) < 4? vowel: consonant), genpasswd, GEN_PWDLEN);
  432.  
  433.     salt[0] = '\0';
  434.     salt[1] = '\0';
  435.     while (!isalpha(salt[0]))
  436.       salt[0] = random() & 0x7f ;
  437.     while (!isalpha(salt[1]))
  438.       salt[1] = random() & 0x7f ;
  439.     if (entry->pw_passwd)
  440.     free(entry->pw_passwd);
  441.     entry->pw_passwd = (char *)malloc(SPACE_PASSWD);
  442.     strcpy(entry->pw_passwd, crypt(genpasswd, salt));
  443.     printf("New password for %s is \"%s\"\n", entry->pw_name, genpasswd);
  444. }
  445.  
  446. /*
  447.  * copy_pwfile1
  448.  *
  449.  * Copy all entries in the password file before the one we are interested in.
  450.  */
  451. struct passwd *
  452. copy_pwfile1(name)
  453.     char *name;
  454. {
  455.     struct passwd *current;
  456.  
  457.     fseek(pwfile, 0, FSEEK_F_BEGIN);
  458.     while (!feof(pwfile)) {
  459.     current = fgetpwent(pwfile);
  460.     if (current && strncmp(current->pw_name, name, PW_NAMLEN) != 0) {
  461.         if (putpwent(current, tmppwfile) != 0) {
  462.         fprintf(stderr, "%s: Cannot put current entry for %0.*s\n",
  463.             whoami, PW_NAMLEN, current->pw_name);
  464.         perror(whoami);
  465.         my_exit(1);
  466.         }
  467.     } else {
  468.         return(current);
  469.     }
  470.     }
  471.     return(NULL);
  472. }
  473.  
  474. /*
  475.  * copy_pwfile2
  476.  *
  477.  * Add the new/modified entry.  Copy all entries in the password file after
  478.  * the one we are interested in.
  479.  */
  480. copy_pwfile2(new)
  481.     struct passwd *new;
  482. {
  483.     struct passwd *current;
  484.  
  485.     if (putpwent(new, tmppwfile) != 0) {
  486.     fprintf(stderr, "%s: Cannot put new entry for %0.*s\n",
  487.         whoami, PW_NAMLEN, new->pw_name);
  488.     perror(whoami);
  489.     my_exit(1);
  490.     }
  491.  
  492.     while (!feof(pwfile)) {
  493.     current = fgetpwent(pwfile);
  494.     if (current && putpwent(current, tmppwfile) != 0) {
  495.         fprintf(stderr, "%s: Cannot put current entry for %0.*s\n",
  496.             whoami, PW_NAMLEN, current->pw_name);
  497.         perror(whoami);
  498.         my_exit(1);
  499.     }
  500.     }
  501. }
  502.  
  503. /*
  504.  * copy_back
  505.  *
  506.  * Copy the new file back into the original file.  Can't use rename,
  507.  * because the files aren't guaranteed to be on the same file system.
  508.  */
  509. copy_back()
  510. {
  511.     char buffer[4096];
  512.     int count;
  513.  
  514.     fseek(pwfile, 0, FSEEK_F_BEGIN);
  515.     fseek(tmppwfile, 0, FSEEK_F_BEGIN);
  516.  
  517.     while ((count = fread(buffer, sizeof(char), 4096, tmppwfile)) > 0)
  518.       fwrite(buffer, sizeof(char), count, pwfile);
  519.  
  520.     fclose(pwfile);
  521.     fclose(tmppwfile);
  522. }
  523.  
  524. /*
  525.  * getuser
  526.  *
  527.  * Get a username and password.  Fail upon end of file.
  528.  */
  529. getuser(name, data)
  530.     char *name, *data;
  531. {
  532.     char buffer[100];
  533.     int i;
  534.  
  535.     name[0] = '\0';
  536.     while (strlen(name) == 0) {
  537.     printf("TACACS Username: ");
  538.     if (!fgets(buffer, 100, stdin) || feof(stdin)) {
  539.         perror(whoami);
  540.         return(0);
  541.     }
  542.     strncpy(name, buffer, PW_NAMLEN);
  543.     for (i = 0; i < PW_NAMLEN; i++)
  544.         if (name[i] == '\n')
  545.         name[i] = '\0';
  546.     name[PW_NAMLEN] = '\0';
  547.     }
  548.  
  549.     strncpy(data, getpass("Old Password: "), PW_PWDLEN);
  550.     if (feof(stdin))
  551.     return(0);
  552.  
  553.     return(1);
  554. }
  555.  
  556. /*
  557.  * waitforuser
  558.  *
  559.  * Wait for the user to say he is done.
  560.  */
  561. waitforuser()
  562. {
  563.     printf("Hit the 'return' key to exit.");
  564.     getchar();
  565. }
  566.  
  567. /*
  568.  * init
  569.  *
  570.  * Initialize the program.
  571.  */
  572. init(argc, argv)
  573.     int argc;
  574.     char **argv;
  575. {
  576.     char *c, *tmppwfilename, template[100];
  577.  
  578.     whoami = argv[0];
  579.  
  580.     /*
  581.      * Open TAC password file, and scratch file
  582.      */
  583.     if (pwfile)
  584.     fclose(pwfile);
  585.     if ((pwfile = fopen(TACPWFILE, "a+")) == NULL) {
  586.     fprintf(stderr, "%s: cannot open TACACS password file\n", whoami);
  587.     perror(whoami);
  588.     my_exit(1);
  589.     }
  590.     strcpy(template, TMPPWFILE);
  591.     tmppwfilename = mktemp(template);
  592.     if (tmppwfile)
  593.     fclose(tmppwfile);
  594.     if ((tmppwfile = fopen(tmppwfilename, "w+")) == NULL) {
  595.     fprintf(stderr, "%s: cannot open temporary file %s\n", whoami, tmppwfilename);
  596.     perror(whoami);
  597.     my_exit(1);
  598.     }
  599.     if (unlink(tmppwfilename) != 0) {
  600.     fprintf(stderr, "%s: cannot unlink temporary file %s\n", whoami, tmppwfilename);
  601.     perror(whoami);
  602.     my_exit(1);
  603.     }
  604.  
  605.     srandom(time(0) + (getpgrp() << 8) + getpid());
  606. }
  607.  
  608. main(argc, argv)
  609.     int argc;
  610.     char **argv;
  611. {
  612.     struct passwd *entry;
  613.     char username[10], password[10];
  614.     int i, done;
  615.  
  616.     for (i = 0; i < 3; i++) {
  617.     init(argc, argv);
  618.  
  619.     if (!getuser(username, password))
  620.         return(1);
  621.  
  622.     entry = copy_pwfile1(username);
  623.     if (!entry) {
  624.         printf("I cannot find a user named '%s' in the TACACS database.\n", username);
  625.         printf("Please contact cisco Enginering for assistance.\n");
  626.         my_exit(1);
  627.     }
  628.  
  629.     if (!valid(entry, username, password)) {
  630.         printf("You typed an incorrect username/password combinaion.\n");
  631.         printf("Please try again\n\n");
  632.         continue;
  633.     }
  634.  
  635.     if (!update_expiration(entry)) {
  636.         printf("You are not allowed to change your password and revalidate\n");
  637.         printf("your account.  Please contact cisco Engineering for assistance.\n");
  638.         my_exit(1);
  639.     }
  640.  
  641.     make_passwd(entry);
  642.     copy_pwfile2(entry);
  643.     copy_back();
  644.     waitforuser();
  645.     my_exit(0);
  646.     }
  647.     my_exit(0);
  648. }
  649.